package de.karlNet.sambamanager.view.beans.forms;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapController.AbstractLdapTree;
import de.karlNet.sambamanager.ldapController.GroupOnlyTree;
import de.karlNet.sambamanager.ldapModel.ITypeCheckerCallback;
import de.karlNet.sambamanager.ldapModel.LdapGroup;
import de.karlNet.sambamanager.ldapModel.LdapObject;
import de.karlNet.sambamanager.view.beans.ManualLeafHandlingTreeNode;

@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class SelectableGroupTree extends SelectableTree {
	@Autowired
	private GroupOnlyTree groupOnlyTree;

	public List<LdapGroup> getSelectedGroups() {
		final List<LdapGroup> ret = new ArrayList<LdapGroup>();
		for (LdapObject ldapObjects : this.getSelectedLdapNestable()) {
			ldapObjects.isType(new ITypeCheckerCallback() {
				@Override
				public void isGroup(LdapGroup ldapGroup) {
					ret.add(ldapGroup);
				}
			});
		}
		return ret;
	}

	@Override
	protected AbstractLdapTree getLdapTree() {
		return this.groupOnlyTree;
	}

	@Override
	protected ITypeCheckerCallback allowedTypes(
			final ManualLeafHandlingTreeNode handlingTreeNode) {
		return new ITypeCheckerCallback() {
			public void isGroup() {
				handlingTreeNode.setSelectable(true);
				handlingTreeNode.setType("group");
			}

			public void isUser() {
				handlingTreeNode.setSelectable(false);
				handlingTreeNode.setType("user");
			}

			public void isNestable() {
				handlingTreeNode.setSelectable(false);
			}

			public void isLdapObject() {
				handlingTreeNode.setSelectable(false);
				handlingTreeNode.setType("ldapObject");
			}

			public void isContainer() {
				handlingTreeNode.setSelectable(true);
				handlingTreeNode.setType("container");
			}
		};
	}
}
